<?php
//验证码**********

//1.发送头,标明当前内容为图片类型
header('Content-type:image/png');
//2.创建画布
$imgW = 200;
$imgH = 60;
$img = imagecreatetruecolor($imgW,$imgH);
//3.调色
$gray = imagecolorallocate($img,216,216,216);
//4.填充画布
imagefill($img,0,0,$gray);
//写字*************
//验证码长度
$len = 4;
//验证码大小
$size = 30;
for ($i = 0; $i < $len; $i++){
    //imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text)
    $x = $imgW / $len * $i + 10;
    $y = ($imgH + $size) / 2;
    //随机字符
    $text = md5(microtime(true));
    //因为MD5之后的值是32位,下标是从0开始的,所以是31
    $text = $text[mt_rand(0,31)];
    $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagettftext($img,$size,mt_rand(-45,45),$x,$y,$color,'./font.ttf',$text);
}
//干扰******************
//矩形边框***
$black = '#000000';
//把16进制的颜色转为10进制
$color = hexdec($black);
imagerectangle($img,0,0,$imgW-1,$imgH-1,$color);

//圆***
for ($i = 0; $i<20;$i++){
    //圆心坐标
    $cx = mt_rand(0,$imgW);
    $cy = mt_rand(0,$imgH);
    //圆的大小
    $wh = mt_rand(0,20);
    //随机颜色
    $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imageellipse($img,$cx,$cy,$wh,$wh,$color);
}

//线***
for ($i = 0; $i < 5; $i++){
    $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imageline($img,mt_rand(0,$imgW),mt_rand(0,$imgH),mt_rand(0,$imgW),mt_rand(0,$imgH),$color);
}
//点***
for ($i = 0; $i < 50; $i++){
    $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagesetpixel($img,mt_rand(0,$imgW),mt_rand(0,$imgH),$color);

}

//5.输出
imagepng($img);
//6.销毁
imagedestroy($img);






